﻿Imports BUS
Imports DTO

Public Class FrmLoaiSanh
    Dim i As Integer = 0
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        load_loaisanh()
        khoa_data()
    End Sub
    Public Sub load_loaisanh()
        Dim db As New DataTable()
        Dim ds As New LOAISANHBUS()
        db = ds.load_loaisanh()
        luoi.DataSource = db
        luoi.[ReadOnly] = True
        ' luoi.Columns["PASS"].Visible = false;
        Dim i As Integer
        For i = 0 To db.Rows.Count - 1
            luoi.Rows(i).Cells(0).Value = (i + 1)

        Next i

        txttenloaisanh.DataBindings.Clear()
        txttenloaisanh.DataBindings.Add("Text", db, "TENLOAISANH")
        txtdongiatoithieu.DataBindings.Clear()
        txtdongiatoithieu.DataBindings.Add("Text", db, "DONGIATOITHIEU")

        txtmasanh.DataBindings.Clear()
        txtmasanh.DataBindings.Add("Text", db, "MALOAISANH")

        

    End Sub
    Public Sub mo_data_them()
        xl.Enabled = True
        txtdongiatoithieu.Text = ""


        txttenloaisanh.Text = ""
        xl1.Visible = False
        xl2.Visible = True
    End Sub
    Public Sub mo_data_capnhat()
        xl.Enabled = True
        xl1.Visible = False
        xl2.Visible = True
    End Sub
    Public Sub khoa_data()

        xl.Enabled = False
        xl1.Visible = True
        xl2.Visible = False
        load_loaisanh()
    End Sub


    Private Sub txtdongiatoithieu_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtdongiatoithieu.KeyPress
        If Not [Char].IsDigit(e.KeyChar) AndAlso Not [Char].IsControl(e.KeyChar) Then
            e.Handled = True
        End If

    End Sub

    

    Private Sub txtsobandutru_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If Not [Char].IsDigit(e.KeyChar) AndAlso Not [Char].IsControl(e.KeyChar) Then
            e.Handled = True
        End If

    End Sub
    Public Function getinfo() As LOAISANHDTO
        Dim test As New kiemtra()
        Dim dto As New LOAISANHDTO()
        ' gan dta
        dto.MALOAISANH = txtmasanh.Text

        If test.ktra_rong(txttenloaisanh.Text) = True Then
            dto.TENLOAISANH = txttenloaisanh.Text
        Else
            MessageBox.Show("Chưa nhập tên loại sảnh")
        End If

        If test.ktra_rong(txtdongiatoithieu.Text) = True Then
            dto.DONGIATOITHIEU = Single.Parse(txtdongiatoithieu.Text.ToString())
        Else
            MessageBox.Show("Chưa nhập đơn giá tối thiểu")
        End If

       


        Return dto
    End Function

    Private Sub butthem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butthem.Click
        i = 1
        mo_data_them()
    End Sub

    Private Sub butCapNhap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butCapNhap.Click
        i = 0
        mo_data_capnhat()
    End Sub

    Private Sub butXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butXoa.Click
        Dim dto As New LOAISANHDTO()
        Dim bus As New LOAISANHBUS()
        dto = getinfo()

        Dim dlr As New DialogResult()
        dlr = MessageBox.Show("Bạn có chắn là xóa mẫu tin này không?", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)
        If dlr = DialogResult.OK Then
            If bus.xoa_loaisanh(dto) = True Then
                MessageBox.Show("Xoa thanh cong")
                load_loaisanh()
            Else
                MessageBox.Show("Du lieu chua xoa duoc, vui long kiem tra lai")

            End If
        End If

    End Sub

    Private Sub butkluu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butkluu.Click
        khoa_data()
        load_loaisanh()
    End Sub

    Private Sub butLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butLuu.Click
        Dim dto As New LOAISANHDTO()
        Dim bus As New LOAISANHBUS()
        dto = getinfo()
        If i = 1 Then
            If bus.them_loaisanh(dto) = True Then
                MessageBox.Show("Them thanh cong")
                load_loaisanh()
                khoa_data()
            Else
                MessageBox.Show("Chua Them duoc")
            End If
        Else
            If bus.capnhat_loaisanh(dto) = True Then
                MessageBox.Show("Cap nhat thanh cong")
                load_loaisanh()
                khoa_data()
            Else
                MessageBox.Show("Chua cap nhat duoc")
            End If
        End If

    End Sub

    Private Sub butThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butThoat.Click
        Me.Close()
    End Sub

    Private Sub txtdongiatoithieu_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtdongiatoithieu.TextChanged

    End Sub
End Class
